//
//  Date+Ext.swift
//  
//
//  Created by bo wang on 2020/9/2.
//  Copyright © 2020 wangbo. All rights reserved.
//

import Foundation

extension Date {
    
    static let generalDateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return formatter
    }()
    
    func dateComponents(to: Date) -> DateComponents {
        let components: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
        return Calendar.default.dateComponents(components, from: self, to: to)
    }
    
    var msgDes: String {
        let formatter = DateFormatter()
        if self.isToday {
            formatter.dateFormat = "HH:mm"
            return formatter.string(from: self)
        } else {
            return Date.generalDateFormatter.string(from: self)
        }
    }
    
    var descriptionToNow: String {
        let c = self.dateComponents(to: Date())
        
        if let year = c.year, year > 0 {
            return Date.generalDateFormatter.string(from: self)
        }
        
        if let month = c.month, month > 0 {
            return Date.generalDateFormatter.string(from: self)
        }
        
        if let day = c.day, day > 0 {
            return "\(day)天前"
        }
        
        if let hour = c.hour, hour > 0 {
            return "\(hour)小时前"
        }
        
        if let minute = c.minute, minute > 0 {
            return "\(minute)分钟前"
        }
        
        return "刚刚"
    }
}
